/*
 * Proja - project management software
 * Copyright (C) 2012  Oleg Kasian
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package org.proja.core.dao;

import org.proja.core.entity.User;
import org.proja.core.entity.enumerations.TokenType;
import org.springframework.stereotype.Component;


/**
 *
 * @author oleg
 */
@Component
public class UserDao extends AbstractDao<User> {

    { setClazz(User.class); }

    public User findByLogin(final String login) {
        return (User) getCurrentSession().createQuery(
                "from User u where lower(u.login) = :login")
                .setEntity("login", login.toLowerCase())
                .uniqueResult();
    }

    public User getWithToken(final TokenType type, final String token) {
        return (User) getCurrentSession().createQuery(
                "from User u join u.tokens t where"
                + " t.type = :type and t.value = :value")
                .setParameter("type", type)
                .setString("value", token)
                .uniqueResult();
    }
}
